package com.dragon.modules.yuegou.service.impl.coupon;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.coupon.CouponTemplateStockEntity;
import com.dragon.modules.yuegou.enums.code.ProductCode;
import com.dragon.modules.yuegou.mapper.coupon.CouponTemplateStockMapper;
import com.dragon.modules.yuegou.service.coupon.CouponTemplateStockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 优惠券模板库存 服务实现
 *
 * @author pbuilder generated
 * @date 2020-06-30 17:39:39
 * @version 2.0
 */
@Service("coupCouponTemplateStockService")
public class CouponTemplateStockServiceImpl
		extends ServiceImpl<CouponTemplateStockMapper, CouponTemplateStockEntity>
		implements CouponTemplateStockService {

	@Override
	@Transactional
	public void editStock(String couponTemplateId, Integer quantity) {
		if (quantity == 0) {
			return;
		}
		if (quantity > 0) {
			plusStock(couponTemplateId, quantity);
		} else {
			reduceStock(couponTemplateId, 0 - quantity);
		}
	}

	@Override
	public void reduceStock(String couponTemplateId, Integer quantity) {
		if (baseMapper.reduceStock(couponTemplateId, quantity) <= 0) {
			throw new DragonException(ProductCode.UNDERSTOCK);
		}
	}

	@Override
	public void plusStock(String couponTemplateId, Integer quantity) {
		if (baseMapper.plusStock(couponTemplateId, quantity) <= 0) {
			throw new DragonException(ProductCode.LOCK_STOCK_FAILED_BG);
		}
	}

}